using Microsoft.CodeAnalysis.Testing;
using System.Collections.Immutable;
using Xunit;

using GeneratorTest = Microsoft.CodeAnalysis.CSharp.Testing.CSharpSourceGeneratorTest<AutoRegisterInject.Tests.SourceGeneratorAdapter<AutoRegisterInject.Generator>, Microsoft.CodeAnalysis.Testing.Verifiers.XUnitVerifier>;

namespace AutoRegisterInject.Tests;

public partial class GenerationTests
{
    private static readonly ReferenceAssemblies Reference = ReferenceAssemblies
        .Net
        .Net60
        .AddPackages(ImmutableArray.Create(new PackageIdentity("Microsoft.Extensions.DependencyInjection", "7.0.0"),
            new PackageIdentity("Microsoft.Extensions.Hosting.Abstractions", "7.0.0")));

    private const string ATTRIBUTE_SOURCE_OUTPUT = @"// <auto-generated>
//     Automatically generated by AutoRegisterInject.
//     Changes made to this file may be lost and may cause undesirable behaviour.
// </auto-generated>
[System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class RegisterScopedAttribute : System.Attribute { }
[System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class RegisterSingletonAttribute : System.Attribute { }
[System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class RegisterTransientAttribute : System.Attribute { }
[System.AttributeUsage(System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
internal sealed class RegisterHostedServiceAttribute : System.Attribute { }";

    private static async Task RunGenerator(string sourceInput, string expectedSourceOutput)
    {
        await new GeneratorTest
        {
            ReferenceAssemblies = Reference,
            TestState =
            {
                Sources = { sourceInput },
                GeneratedSources =
                {
                    (typeof(SourceGeneratorAdapter<Generator>), "AutoRegisterInject.Attributes.g.cs", ATTRIBUTE_SOURCE_OUTPUT),
                    (typeof(SourceGeneratorAdapter<Generator>), "AutoRegisterInject.ServiceCollectionExtension.g.cs", expectedSourceOutput),
                },
            },
        }.RunAsync();
    }

    [Fact]
    public async Task ShouldGenerateAttributesAndServiceCollectionExtensions()
    {
        const string EXPECTED = @"// <auto-generated>
//     Automatically generated by AutoRegisterInject.
//     Changes made to this file may be lost and may cause undesirable behaviour.
// </auto-generated>
using Microsoft.Extensions.DependencyInjection;
public static class AutoRegisterInjectServiceCollectionExtension
{
    public static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegisterFromTestProject(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        return AutoRegister(serviceCollection);
    }

    internal static Microsoft.Extensions.DependencyInjection.IServiceCollection AutoRegister(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection)
    {
        
        return serviceCollection;
    }
}";

        await RunGenerator(string.Empty, EXPECTED);
    }
}